include (ExternalProject)

set_property (DIRECTORY PROPERTY EP_BASE Dependencies)

set (DEPENDENCIES)
set (EXTRA_CMAKE_ARGS)
set (proj BZip2)
list (APPEND DEPENDENCIES ${proj})

set(EP_SOURCE_DIR ${CMAKE_BINARY_DIR}/${proj})
set(EP_BINARY_DIR ${CMAKE_BINARY_DIR}/${proj}-build)
set(EP_INSTALL_DIR ${CMAKE_BINARY_DIR}/${proj}-install)

ExternalProject_Add(${proj}
    ${${proj}_EP_ARGS}
    GIT_REPOSITORY https://github.com/commontk/bzip2.git
    GIT_TAG        0e735f23032ececcf52ed49b27928390fff28e50
    SOURCE_DIR     ${EP_SOURCE_DIR}
    BINARY_DIR     ${EP_BINARY_DIR}
    INSTALL_DIR    ${EP_INSTALL_DIR}
    CMAKE_CACHE_ARGS
      #-DCMAKE_CXX_COMPILER:FILEPATH=${CMAKE_CXX_COMPILER}
      -DCMAKE_C_COMPILER:FILEPATH=${CMAKE_C_COMPILER}
      #-DCMAKE_C_FLAGS:STRING=${ep_common_c_flags}
      -DCMAKE_DEBUG_POSTFIX:STRING=
      -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
  )

list (APPEND EXTRA_CMAKE_ARGS
  -DBOOST_ROOT=${CMAKE_CURRENT_BINARY_DIR}/Dependencies/Source/ep_boost
  -DBoost_NO_SYSTEM_PATHS=ON)

  set(BZIP2_INCLUDE_DIR ${EP_SOURCE_DIR})
  if(WIN32)
    set(BZIP2_LIBRARIES ${EP_BINARY_DIR}/lib/$(Configuration)/libbz2.lib)
  else()
    set(BZIP2_LIBRARIES ${EP_BINARY_DIR}/lib/libbz2.a)
  endif()
  message(status "SuperBuild BZIP2_LIBRARIES: ${BZIP2_LIBRARIES}")

  #set(BZIP2_LIBRARIES_DIR ${EP_INSTALL_DIR})

  list (APPEND EXTRA_CMAKE_ARGS
  -DBZIP2_INCLUDE_DIR:PATH=${BZIP2_INCLUDE_DIR}
  -DBZIP2_LIBRARIES:FILEPATH=${BZIP2_LIBRARIES})

  #message(status "SuperBuild DBZIP2_LIBRARIES_DIR: ${BZIP2_LIBRARIES_DIR}")
  
# FIXME add to default target "all"?
ExternalProject_Add (ep_TestBZip2
  DEPENDS ${DEPENDENCIES}
  SOURCE_DIR ${PROJECT_SOURCE_DIR}
  CMAKE_ARGS -DUSE_SUPERBUILD=OFF ${EXTRA_CMAKE_ARGS}
  INSTALL_COMMAND ""
  BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/blah)